package com.baichang.taoshunda.dao.impl;

import com.baichang.framework.model.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.baichang.framework.dao.impl.BaseDaoImpl;
import com.baichang.taoshunda.dao.WaiterTypeDao;
import com.baichang.taoshunda.entity.WaiterType;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class WaiterTypeDaoImpl extends BaseDaoImpl<WaiterType> implements WaiterTypeDao {
    /**
     * 接口查询所有的本地服务
     *
     * @param isHome
     * @return
     */
    @Override
    public List<Map<String, Object>> getwaiterType(int isHome) {
        String sql = null;
        if (isHome == 1) {
            sql = "SELECT * FROM `t_waiter_type` WHERE state=1 AND is_home=1 ";
        } else {
            sql = "SELECT * FROM `t_waiter_type` WHERE state=1 ";
        }
        sql += " ORDER BY sorting DESC";
        return queryForMapList(sql);
    }

    /**
     * 查询本地服务列表
     *
     * @param name
     * @param nowPage
     * @param pageSize
     * @return
     */
    @Override
    public Page queryLifeType(String name, int nowPage, int pageSize) {
        List list = new ArrayList();
        String sql = "SELECT * FROM t_waiter_type WHERE state IN (1) ";
        if (StringUtils.isNotBlank(name)) {
            sql += " AND waiter_name LIKE ?";
            list.add("%" + name + "%");
        }
        sql += " ORDER BY sorting DESC ";
        return pageForBeanList(sql, nowPage, pageSize, list.toArray());
    }

    /**
     * 删除
     *
     * @param id
     * @param state
     * @return
     */
    @Override
    public int updateLifeState(Integer id, Integer state) {
        String sql = "UPDATE {table} SET state=? WHERE id=?";
        return update(sql, state, id);
    }

    /**
     * 修改服务分类
     *
     * @param map
     * @param id
     * @return
     */
    @Override
    public int update(Map map, Integer id) {
        String sql = "id=?";
        return update(sql, map, id);
    }

    /**
     * 查询本地服务商家类型
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> queryWaiterTypeId() {
        String sql = "SELECT * FROM {table} WHERE state=1";
        return queryForMapList(sql);
    }

    /**
     * 获取分类名称
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getTypeName() {
        String sql = "SELECT waiter_name AS name FROM {table} WHERE state=1";
        return queryForMapList(sql);
    }

    /**
     * 首页推荐
     *
     * @param id
     * @param isHome
     * @return
     */
    @Override
    public int updateIsHome(Integer id, Integer isHome) {
        String sql = " UPDATE {table} SET is_home=? WHERE id=? ";
        return update(sql, isHome, id);
    }

    /**
     * 修改排序
     *
     * @param id
     * @param sorting
     * @return
     */
    @Override
    public int updateSorting(Integer id, Integer sorting) {
        String sql = " UPDATE {table} SET sorting=? WHERE id=? ";
        return update(sql, sorting, id);
    }

    /**
     * 查询服务分类
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> queryWaiterType() {
        String sql = "SELECT * FROM {table} WHERE state=1 ORDER BY sorting DESC";
        return queryForMapList(sql);
    }
}